package com.liuyk.commonlib.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.liuyk.commonlib.R;
import com.liuyk.commonlib.dialog.OtherDialogUtils;
import com.liuyk.commonlib.dialog.ToastUtils;
import com.liuyk.commonlib.widget.TitleBar;

/**
 * activity base
 * <p>
 * Created by liuyk on 2019/10/16.
 */
public abstract class BaseActivity extends BaseAppCompatActivity implements View.OnClickListener, TitleBar.TitleClickListener {

    private View base_parent_contain;
    private ViewGroup mContainer;

    private TitleBar mTitleBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.base_main_activity_layout);
        baseInitView();
    }

    private void baseInitView() {
        base_parent_contain = findViewById(R.id.base_parent_contain);
        mContainer = findViewById(R.id.container);
        mTitleBar = findViewById(R.id.base_title_bar);
        if (mTitleBar != null) {
            mTitleBar.setTitleBarLeftIcon(R.mipmap.back_black);
            mTitleBar.setTitleClickListener(this);
            mTitleBar.setShowToolBar(isShowToolBar());
        }
    }

    @Override
    public void notchSetting(int left, int notchHeight, int right, int bottom) {
        if (isShowToolBar()) {
            setTitleBarPadding(left, notchHeight, right, bottom);
        }
    }

    public void setTitleBarPadding(int left, int top, int right, int bottom) {
        if (isShowToolBar()) {
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mTitleBar.getLayoutParams();
            layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
            mTitleBar.setPadding(left, top, right, bottom);
        }
    }

    @Override
    public boolean isStatusLightMode() {
        return true;
    }

    @Override
    protected int getLayout() {
        return R.layout.base_main_activity_layout;
    }

    protected void fullscreen() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    protected void showToast(String msg) {
        ToastUtils.showToast(this, msg);
    }

    protected void showToast(int resId) {
        showToast(getString(resId));
    }

    /**
     * set layout
     *
     * @param resId resId
     */
    public void setContentLayout(int resId) {
        View contentView = LayoutInflater.from(this).inflate(resId, mContainer, false);
        setContentLayout(contentView);
    }

    protected void setContentLayout(View view) {
        if (mContainer != null) {
            mContainer.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        }
    }

    protected void setLeftIcon(int resId) {
        if (mTitleBar != null) {
            mTitleBar.setTitleBarLeftIcon(resId);
        }
    }

    protected void setLeftIcon(Drawable drawable) {
        if (mTitleBar != null) {
            mTitleBar.setTitleBarLeftIcon(drawable);
        }
    }

    protected void setTopBarTitle(String title) {
        if (mTitleBar != null) {
            mTitleBar.setTitleBarTitle(title);
        }
    }

    protected void setTopBarTitle(int resId) {
        setTopBarTitle(getString(resId));
    }

    protected void setTitleBarTextColor(int color) {
        if (mTitleBar != null) {
            mTitleBar.setTitleBarTextColor(color);
        }
    }

    protected void setTitleTextSize(float size) {
        if (mTitleBar != null) {
            mTitleBar.setTitleBarTextSize(size);
        }
    }

    public void setTopBarRight(String msg) {
        if (mTitleBar != null) {
            mTitleBar.setTitleBarRightText(msg);
        }
    }

    public void setTopBarRight(int resId) {
        setTopBarRight(getString(resId));
    }

    protected void setTopBarRightColor(int resId) {
        if (mTitleBar != null) {
            mTitleBar.setTitleBarRightColor(resId);
        }
    }

    protected void setTitleRightIcon(int resId) {
        if (mTitleBar != null) {
            mTitleBar.setTitleBarRightIcon(resId);
        }
    }

    public void hideTitleBar() {
        if (mTitleBar != null) {
            mTitleBar.hideTitleBar();
        }
    }

    public void showTitleBar() {
        if (mTitleBar != null) {
            mTitleBar.showTitleBar();
        }
    }

    protected void showProgress(String message) {
        OtherDialogUtils.showLoading(this, message);
    }

    protected void dismissProgress() {
        OtherDialogUtils.dismissLoading();
    }

    protected abstract boolean isShowToolBar();

    public void setBaseParentBackground(int resId) {
        if (base_parent_contain != null) {
            base_parent_contain.setBackgroundResource(resId);
        }
    }

    public void setBaseParentBackgroundColor(int color) {
        if (base_parent_contain != null) {
            base_parent_contain.setBackgroundColor(getResources().getColor(color));
        }
    }

    /**
     * alpha
     *
     * @param bgAlpha alpha 0.0 - 1.0
     */
    protected void windowToAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onClick(View v) {
        //child class super.onClick();
    }

    //标题栏返回键
    protected void titleBackClick(View v) {
        finish();
    }

    //标题栏右边文字 点击事件
    protected void titleRightTextClick(View v) {

    }

    //标题栏右边icon 点击事件
    protected void titleRightIconClick(View v) {

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    protected Activity getActivity() {
        return this;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public void titleBarLeftClick(View v) {
        titleBackClick(v);
    }

    @Override
    public void titleBarRightTextClick(View v) {
        titleRightTextClick(v);
    }

    @Override
    public void titleBarRightIconClick(View v) {
        titleRightIconClick(v);
    }

    public TitleBar getTitleBar() {
        return mTitleBar;
    }
}
